package com.mycode;

import java.io.*;

/**
 * @author shkstart
 * @create 2021-01-21 16:34
 */
public class TestPoker {
    public static void main(String[] args) {
        // 写Poker对象
        writeFile("fileDir/dasd.txt", new Poker(1, 5));
        // 读Poker对象
        readFile("fileDir/dasd.txt");
    }

    private static void readFile(String filePath) {
        if (filePath == null || filePath.trim().isEmpty()) {
            System.out.println("Error: 输入无效");
            return;
        }
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.println("Error:不存在当前文件");
            return;
        }
        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            // 文件通道关联物理文件
            fileInputStream = new FileInputStream(file);
            // 对象通道关联文件通道
            objectInputStream = new ObjectInputStream(fileInputStream);
            while (true) {
                Poker poker = (Poker) objectInputStream.readObject();
                System.out.println(poker);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (EOFException e) {
            System.out.println("读到文件尾，正常结束...");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private static void writeFile(String filePath, Poker poker) {
        if (filePath == null || filePath.trim().isEmpty() || poker == null) {
            System.out.println("Error:输入无效");
            return;
        }

        File file = new File(filePath);

        File dir = file.getParentFile();

        if (!dir.exists()) {
            dir.mkdirs();
        }
        // 建立文件通道
        FileOutputStream fileOutputStream = null;
        // 建立对象输出通道
        ObjectOutputStream objectOutputStream = null;

        try {
            fileOutputStream = new FileOutputStream(file, true);
            if (file.length() > 0) {
                objectOutputStream = new MyObjectOutputStream_2(fileOutputStream);
            } else {
                objectOutputStream = new ObjectOutputStream(fileOutputStream);
            }
            // 写对象
            objectOutputStream.writeObject(poker);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                if (fileOutputStream != null) {

                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
